//
//  YHWelcomeViewController.swift
//  WeiBoYH
//
//  Created by yanhui on 2017/1/9.
//  Copyright © 2017年 yanhui. All rights reserved.
//

import UIKit
import SDWebImage

private let iconHeight: CGFloat = 90
private let bottomMargin: CGFloat = 160

class YHWelcomeViewController: UIViewController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        setupUI()
    }
    
    
    //将动画效果放在viewDidAppear阶段执行
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func setupUI() {
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        //SnapKit
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-bottomMargin)
            make.size.equalTo(CGSize(width: iconHeight, height: iconHeight))
        }
        
        welcomeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(iconView.snp.bottom).offset(20)
        }
        
        //切圆角
        iconView.layer.cornerRadius = iconHeight / 2
        iconView.layer.masksToBounds = iconView.layer.cornerRadius > 0
        //给icon设置头像
      
        iconView.sd_setImage(with: YHUserAccountViewModel.shard.headUrl)
        
    }
    
    //动画效果
    private func startAnimation() {
        let offset = -(ScreenHeight - iconHeight - bottomMargin)
        
        self.iconView.snp.updateConstraints({ (make) in
            make.bottom.equalTo(self.view).offset(offset)
        })
        
        UIView.animate(withDuration: 1.2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: {
            
            //强制跟新约束
            self.view.layoutIfNeeded()
        }, completion: { (_) -> () in
            UIView.animate(withDuration: 0.5, animations: {
                self.welcomeLabel.alpha = 1
            }, completion: { (_) in
                //执行页面的跳转
                //发送通知
                NotificationCenter.default.post(name: NSNotification.Name.AppChangeRootVc, object:"YHTabBarController" )
                
            })
        })
    }
               //用户头像
    lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default"))
              //欢迎文字
    lazy var welcomeLabel: UILabel = {
        let l = UILabel(text: "\(YHUserAccountViewModel.shard.account?.screen_name ?? "")欢迎归来", fontSize: 15, textColor: UIColor.darkGray)
        l.alpha = 0
        return l
    }()
    
}

